package peishui.views.adapters;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xkh.xkh.R;

import java.util.ArrayList;

import com.xkh.xkh.MyApplication;
import peishui.network.http.GetRealCannelDam;
import shuiyuqing.model.BasicInfoModel;
import shuiyuqing.network.beans.IrrBAWaterGate;
import shuiyuqing.network.beans.IrrBTCanalDetail;
import shuiyuqing.network.beans.RealTimeIrrBRWaterDam;

/**
 * 闸门信息
 * Created by 凯 on 2015/11/10.
 */
public class GateAdapter extends BaseAdapter {

    IrrBTCanalDetail canalDetail;

    // 获取到闸门
    ArrayList<IrrBAWaterGate> waterGates;

    // 与闸门相关的实时数据
    GetRealCannelDam dam;

    public ArrayList<IrrBAWaterGate> getWaterGates() {
        return waterGates;
    }

    //    获取网络请求后
    public void setDam(GetRealCannelDam dam) {
        this.dam = dam;

    }

    public void setCanalDetail(IrrBTCanalDetail canalDetail) {
        this.canalDetail = canalDetail;
        waterGates = (ArrayList<IrrBAWaterGate>) BasicInfoModel.getGatesByCannal(canalDetail.getCanalCode());

    }

    @Override
    public int getCount() {
        return waterGates.size();
    }

    @Override
    public Object getItem(int position) {
        return waterGates.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        IrrBAWaterGate gate = waterGates.get(position);

        convertView = View.inflate(MyApplication.share(), R.layout.item_gatedam,null);
        TextView tv_gateName = (TextView) convertView.findViewById(R.id.id_tv_gateName);
        TextView tv_upwater = (TextView) convertView.findViewById(R.id.id_tv_upwaterValue);
        TextView tv_downwater = (TextView) convertView.findViewById(R.id.id_tv_downwaterValue);
        TextView tv_flow = (TextView) convertView.findViewById(R.id.id_tv_flowValue);
        TextView tv_open = (TextView) convertView.findViewById(R.id.id_tv_openDotvalue);

        tv_gateName.setText(gate.getWatergateName());

        // 进行网络请求后
        if (dam != null)
        {
            GetRealCannelDam.Download.GateWithDam gateWithDam = dam.getDownload().getResult().get(position);
            if (!gateWithDam.getWaterDams().isEmpty())
            {
                RealTimeIrrBRWaterDam waterDam = gateWithDam.getWaterDams().get(0);
                tv_upwater.setText(waterDam.getHeightUp()+"");
                tv_downwater.setText(waterDam.getHeightDown()+"");
                tv_flow.setText(waterDam.getFluxGate()+"");
                tv_open.setText(waterDam.getGateOpenCount()+"");
                convertView.setTag(R.id.id_gateDam,waterDam);
            }

        }

        convertView.setTag(R.id.id_gate,gate);

        return convertView;
    }
}
